package com.mxprojects.gui
{
	import com.mxprojects.admin.AdminPosts;
	import com.mxprojects.constructors.ButtonSmall;
	import com.mxprojects.io.BoardCookie;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.Permissions;
	import com.mxprojects.io.ServerHandler;
	import com.mxprojects.io.TagParser;
	import com.mxprojects.io.UserControl;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.CreatePost;
	import com.mxprojects.views.LoginDialog;
	import com.mxprojects.views.Posts;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	
	public class PostOptions extends BaseForumElement
	{					
		private var postItem:PostItem;
		private var quoteButton:ButtonSmall;
		private var editButton:ButtonSmall;
		private var saveButton:ButtonSmall;
		private var cancelButton:ButtonSmall;
		private var postID:int;
		private var authorID:int;
		private var serverHandler:ServerHandler;
		private var adminOptions:AdminPosts;
				
		public function PostOptions() {}
		
		public function display(postID:int, authorID:int):void {
			//setup the postItem as parent thang
			postItem = parent as PostItem;
			
			//set the local vars
			this.postID = postID;
			this.authorID = authorID;
			
			//add the buttons
			if(Permissions.isAllowedToPost == 0){
				setupButtons(false);
			}else{
				//make sure that we are not in a read-only situation
				if(Posts.isReadOnly == 1 && Permissions.isAdmin == 0 && Permissions.isModerator == 0){
					//gotta hide them buttons since they don't have access
					setupButtons(false);
				}else{
					setupButtons(true);
				}
			}
			
			//parse the default CSS
			super.init(".postOptionsBackground");
		}
		
		override public function resize():void {
			//stretch the bg
			background.width = postItem.background.width - x;
			
			//move the buttons around
			quoteButton.x = Math.floor(background.width - quoteButton.width) - Config.TEXT_PADDING;
			quoteButton.y = Math.round(background.height/2 - quoteButton.height/2);
						
			if(editButton != null){
				editButton.x = quoteButton.x - editButton.width - Config.TEXT_PADDING;
				editButton.y = quoteButton.y;
				
				cancelButton.x = Math.floor(background.width - cancelButton.width) - Config.TEXT_PADDING;
				cancelButton.y = quoteButton.y;
				
				saveButton.x = cancelButton.x - saveButton.width - Config.TEXT_PADDING;
				saveButton.y = quoteButton.y;
				
				//see if there are admin options to move
				if(adminOptions != null){
					adminOptions.x = editButton.x - adminOptions.width - Config.TEXT_PADDING;
					if(saveButton.visible){
						adminOptions.x = saveButton.x - adminOptions.width - Config.TEXT_PADDING;
					}
				}
			}
			
			//we are not using the super.resize because of the way this thing sits
			//see if there was a bg image
			var bgImage:DisplayObject = getChildByName("backgroundImage");
			if(bgImage != null){
				bgImage.width = background.width;
			}
		}
		
		public function setupButtons(isEnabled:Boolean):void {
			//setup the default buttons
			//this is used when logging in so save/cancel don't apply here
			if(quoteButton == null){
				quoteButton = new ButtonSmall(Language.main(Word.QUOTE), quote);
				addChild(quoteButton);
			}
			quoteButton.enabled = isEnabled;
			
			//add an edit button if they have the perms
			if(String(authorID) == BoardCookie.getUserID() || Permissions.isAdmin == 1 || Permissions.isModerator == 1){
				if(editButton == null){
					editButton = new ButtonSmall(Language.main(Word.EDIT), edit);
					addChild(editButton);
					
					saveButton = new ButtonSmall(Language.main(Word.SAVE), save);
					addChild(saveButton);
					
					cancelButton = new ButtonSmall(Language.main(Word.CANCEL), cancel);
					addChild(cancelButton);
				}
				editButton.enabled = isEnabled;
				
				//hide the save/cancel by default
				showSaveButtons(false);
			}
			
			//handle the admin/mod buttons (eg. delete)
			if(Permissions.isAdmin == 1 || Permissions.isModerator == 1){
				if(adminOptions == null){
					adminOptions = new AdminPosts();
					addChild(adminOptions);
					adminOptions.display(postID, authorID);
					
					adminOptions.x = editButton.x - adminOptions.width - Config.TEXT_PADDING;
				}
			}
			
			resize();
		}
		
		private function quote(event:MouseEvent):void {
			//someone wants to quote a message
			//see if they are logged in
			if(UserControl.loggedIn){
				var quoteText:String = "[quote=" + postItem.author.text + "]" + postItem.getOriginalMessage() + "[/quote]\n\n";
				CreatePost.create(CreatePost.ACTION_POST_REPLY, Posts.postData.forumID, Posts.postData.forumTitle, Posts.postData.threadID, Posts.postData.threadTopic, quoteText);
			}else{
				LoginDialog.show();
			}
		}
		
		private function edit(event:MouseEvent):void {
			//edit in-line the message. fancy.
			
			//make the text editable
			changeMessageBox(true);
			
			//put the cursor at the end
			stage.focus = postItem.message;
			postItem.message.setSelection(postItem.message.length, postItem.message.length);
			
			//resize the posts
			resizePosts();
			
			//resize buttons
			//resize();
		}
		
		private function save(event:MouseEvent):void {
			//send the new message to the server			
			var dataToSend:Object = new Object();
			dataToSend.postID = postID;
			dataToSend.authorID = authorID;
			dataToSend.username = BoardCookie.getUser();
			dataToSend.password = BoardCookie.getPassword();
			dataToSend.newMessage = postItem.message.text;
			dataToSend.guestGroupID = Config.GUEST_GROUP_ID;
			
			serverHandler = new ServerHandler();
			serverHandler.transmitData("editpost.php", dataToSend);
			serverHandler.listenForData(checkEdit);
			
			//disable the save and cancel buttons
			saveButton.enabled = false;
			cancelButton.enabled = false;
		}
		
		private function cancel(event:MouseEvent):void {			
			//set the edit box
			changeMessageBox(false);
			
			//snap things back to where they were
			resizePosts();
		}
		
		private function changeMessageBox(editMode:Boolean):void {
			//this just changes the text box from it's dynamic form to input and back again
			
			if(editMode){
				
				//replace the text with the original message
				postItem.message.styleSheet = null;
				postItem.message.htmlText = "";
				postItem.message.text = TagParser.convertToText(postItem.getOriginalMessage());
				
				postItem.message.type = TextFieldType.INPUT;
				postItem.message.autoSize = TextFieldAutoSize.LEFT;
				postItem.message.addEventListener(Event.CHANGE, resizePosts, false, 0, true);
				
				//change the background for edit
				Styles.clearBackgroundImage(postItem.background);
				Styles.setProperties(postItem.background, ".postEditBackground");
				
				//remove any emoticons
				Emoticons.remove(postItem);
			}else{
				//replace the text with the original message
				postItem.message.styleSheet = Styles.css;
				postItem.message.htmlText = TagParser.convertToHtml(postItem.getOriginalMessage());
				
				postItem.message.type = TextFieldType.DYNAMIC;
				postItem.message.autoSize = TextFieldAutoSize.NONE;
				postItem.message.removeEventListener(Event.CHANGE, resizePosts);
				
				//reset the background
				Styles.clearBackgroundImage(postItem.background);
				Styles.setProperties(postItem.background, ".postBackground");
				
				//put the emoticons back
				Emoticons.parse(postItem.message, PostItem.BACKGROUND_COLOR, "postMessage");
			}
			
			//set the edit mode of the post item
			postItem.isEditMode = editMode;
			
			//show the save/cancel buttons
			showSaveButtons(editMode);
		}
		
		private function checkEdit(event:CustomEvent):void {
			//this will make sure that the user edit didn't fail
			var serverData:Object = event.getServerData();
			
			switch(serverData.output){
				case "success":
					//remove the edit look and parse the new message
					postItem.setOriginalMessage(postItem.message.text);
					
					//show the real message parsed with the new data
					changeMessageBox(false);
				break;
				
				case "permissionError":
					DefaultDialog.show(Language.main(Word.ERROR_EDIT_PERMISSIONS));
					
					//re-enable the save and cancel buttons
					saveButton.enabled = true;
					cancelButton.enabled = true;
				break;
				
				case "mySqlError":
					DefaultDialog.show(Language.main(Word.ERROR_MYSQL) + "<br>" + serverData.error);
					
					//re-enable the save and cancel buttons
					saveButton.enabled = true;
					cancelButton.enabled = true;
				break;
				
				case "badLogin":
					//throw an error
					DefaultDialog.show(Language.main(Word.ERROR_LOGIN_INVALID));
					
					//re-enable the save and cancel buttons
					saveButton.enabled = true;
					cancelButton.enabled = true;
				break;
			}
			
			//resize if the posts if need be
			resizePosts();
		}
		
		private function resizePosts(event:Event = null):void {
			Resizer.resizeContent();
		}
		
		private function showSaveButtons(show:Boolean):void {
			//this will toggle the save and cancel buttons from the rest
			saveButton.visible = show;
			cancelButton.visible = show;
			
			quoteButton.visible = !show;
			editButton.visible = !show;
			
			//make sure the buttons are enabled
			saveButton.enabled = true;
			cancelButton.enabled = true;
		}
	}
}